const express = require('express');

const app = express();
const fs = require('fs');
const path = require('path');


//创建一个全局中间件
function globalMiddleWare(req,res,next) {
    let {ip,url} = req;
    fs.appendFileSync(path.resolve(__dirname + '/accsee.log'),`${url} ${ip}\r\n`)
    console.log(ip,url);
    //执行完之后一定要执行这个 要不请求回调函数中的代码将无法执行
    next();
}

app.use(globalMiddleWare);
app.get('/home' ,(req, res) => {
    res.send('home');
});

app.get('/admin' ,(req, res) => {
    res.send('admin');
});

app.all('*' ,(req, res) => {
    res.send('<h1>404 Not Found</h1>');
});

app.listen(3000,() => {
    console.log('服务已启动 3000端口正在监听....');
});